package com.ljf.framework.log;

import com.ljf.framework.cache.LjfCacheConstants;
import com.ljf.framework.cache.LjfCacheManager;
import com.ljf.framework.context.LjfContextManager;
import com.ljf.framework.util.ObjectUtil;

import java.util.UUID;

/**
 * 说明：
 *
 * @Auther: lijinfeng
 * @Date: 2024/5/6
 */
public class LjfLogManager {

    private static volatile  LjfLogService ljfLogService;

    public static LjfLogService getLjfLogService(){
        if (ljfLogService == null) {
            synchronized (LjfLogManager.class) {
                if (ljfLogService == null) {
                    return new LjfLogServiceDefaultImpl();
                }
            }
        }
        return ljfLogService;
    }

    public static void setLjfLogService(LjfLogService ljfLogService){
        LjfLogManager.ljfLogService = ljfLogService;
    }


    public static void createLjfLogId(){
        String uuid = UUID.randomUUID().toString();
        LjfContextManager.getStorage().set("LJF_LOG_ID",uuid);
    }

    public static LjfLogService getLogger(Class c){
        Object object = LjfCacheManager.getObject(LjfLog.LOG_CACHE_PREFIX + c.getName());
        if(ObjectUtil.isEmpty(object)){
            LjfLogService ljfLogService = getLjfLogService();
            ljfLogService.setLogName(c.getName());
            LjfCacheManager.setObject(LjfLog.LOG_CACHE_PREFIX +  c.getName(),ljfLogService, LjfCacheConstants.CACHE_NEVER_TIMEOUT);
            return ljfLogService;
        }
        return (LjfLogService)object;
    }




}
